import javax.swing.*;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreePath;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;

public class frame extends JFrame {
    JTree jTree = null;

    public frame() {
        ;
        setTitle("ID3决策树");
        setSize(400, 500);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        init();
        setVisible(true);
    }

    private void init() {
        JButton jButton_file = new JButton("选择数据源文件");

        final JScrollPane jScrollPane = new JScrollPane();
//        setLayout(new FlowLayout());
        setLayout(null);
        jButton_file.setBounds(110, 0, 180, 30);
        jScrollPane.setBounds(10, 40, 370, 360);
        jScrollPane.setBorder(BorderFactory.createTitledBorder("决策树"));
        final JTextField jTextField = new JTextField();
        jTextField.setBounds(10, 410, 370, 40);
        jTextField.setBorder(BorderFactory.createTitledBorder("路径"));
        jTextField.setEditable(false);


        add(jButton_file);
        add(jScrollPane);
        add(jTextField);
        setResizable(false);
        getContentPane().setBackground(Color.white);
        jScrollPane.setOpaque(false);
        jTextField.setOpaque(false);
//        getContentPane().setBackground(Color.blue);

        jButton_file.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser jFileChooser = new JFileChooser(".");
                int result = jFileChooser.showDialog(null, null);
                if (result == JFileChooser.APPROVE_OPTION) {
                    File file = jFileChooser.getSelectedFile();
                    ID3_tree id3_tree = new ID3_tree(file);
                    jTree = id3_tree.getJTree();
                    DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) jTree.getCellRenderer();
                    renderer.setLeafIcon(null);
                    renderer.setClosedIcon(null);
                    renderer.setOpenIcon(null);
                    jScrollPane.setViewportView(jTree);
                    jTree.addMouseListener(new MouseAdapter() {
                        @Override
                        public void mouseClicked(MouseEvent e) {

                            super.mouseClicked(e);
                            TreePath path = jTree.getSelectionPath();
                            if (path != null) {
                                jTextField.setText(path.toString());
                            }
                        }
                    });
                }
            }
        });
    }

    public static void main(String[] args) {
        new frame();
    }
}
